using LDNS.SDK.Commands;
using LDNS.SDK.Commands.Models;
using NLog;

namespace LDNS.Commands
{
    /// <summary>
    /// 命令处理器基类
    /// 为所有命令处理器提供通用功能
    /// 作者：huobing
    /// </summary>
    public abstract class CommandHandlerBase<TCommand, TResult> : ICommandHandler where TCommand : BaseCommand<TResult> where TResult : BaseCommandResult, new()
    {
        /// <summary>
        /// 日志记录器
        /// </summary>
        protected Logger _logger;
        
        /// <summary>
        /// 命令名称
        /// </summary>
        public abstract string CommandName { get; }
        
        /// <summary>
        /// 构造函数
        /// </summary>
        protected CommandHandlerBase()
        {
            _logger = LogManager.GetCurrentClassLogger();
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数数组</param>
        /// <returns>命令执行结果</returns>
        public BaseCommandResult Execute(string[] args, CommandPermission commandPermission) { 
            return Execute(args);
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数数组</param>
        /// <returns>命令执行结果</returns>
        public abstract BaseCommandResult Execute(string[] args);
        
        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="message">成功消息</param>
        /// <returns>成功的命令结果</returns>
        public TResult CreateSuccessResult(string? message = "")
        {
            return BaseCommandResult.CreateSuccessResult<TResult>(message ?? "", CommandName);
        }
        
        /// <summary>
        /// 创建错误结果
        /// </summary>
        /// <param name="errorMessage">错误消息</param>
        /// <returns>错误的命令结果</returns>
        public BaseCommandResult CreateErrorResult(string errorMessage)
        {
            return BaseCommandResult.CreateErrorResult<TResult>(errorMessage, CommandName);
        }
    }
}